<?php
namespace Home\Controller;
use Think\Controller;
class UserAddressController extends Controller 
{
    public function add() {
        if(IS_POST)
        {
            $model = D('Admin/UserAddress');
            if($model->create(I('post.'), 1))
            {
                if($id = $model->add())
                {
                    $this->success('添加成功！', U('lst?p='.I('get.p')));
                    exit;
                }
            }
            $this->error($model->getError());
        }

        // 设置页面中的信息
        $this->assign(array(
            '_page_title' => '添加',
            '_page_btn_name' => '列表',
            '_page_btn_link' => U('lst'),
        ));
        $this->display();
    }

    public function edit(){

    	$id = I('get.id');

    	if(IS_POST)
    	{
    		$model = D('UserAddress');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('p' => I('post.p', 1))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = D('Admin/UserAddress');
    	$data = $model->search();
        $info = $model->find(I('get.id'));

        if(IS_AJAX){
            echo json_encode($info);
        } else {
            $this->assign(array(
                'data'=> $data['data'],
                'info'=> $info,
            ));
            // 设置页面中的信息
            $this->assign(array(
                '_page_title' => '修改',
                '_page_btn_name' => '列表',
                '_page_btn_link' => U('lst'),
            ));
            $this->display('My/address');
        }
    }

    public function delete(){
    	$model = D('UserAddress');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {
        $goodsId = I('get.id');
        $memberId = session('m_id');

        $addressModel = D('Admin/UserAddress');
        $addressData = $addressModel->search();
        if (IS_AJAX){
            echo json_encode($addressData['data']);
        }else{
            // 设置页面中的信息
            $this->assign(array(
                '_page_title' => '列表',
                '_page_btn_name' => '添加',
                '_page_btn_link' => U('add'),
            ));
            $this->display('my/address');
        }
    }

    public function region() {
        $regionId = I('get.id',1);
        $regionModel = M('Region');
        $regionData = $regionModel->where(array(
            'parent_id' => array('eq', $regionId),
        ))->select();
        echo json_encode($regionData);
    }

    //设置默认地址
    public function setDefault() {
        $id = I('get.id',0);
        $m_id = session('m_id');
        $model = D('Admin/UserAddress');
        if (!$m_id){
            $this->error('请登录!',U('Member/login'));
        }
        $model->where(array(
            'member_id' => array('eq',$m_id),
        ))->save(array('is_default'=> 0));
        $model->where(array(
            'id'=> array('eq',$id),
            'member_id'=>array('eq',$m_id)
        ))->save(array('is_default'=> 1));
        $this->success('设置成功!',U('lst'));
    }
}